from openpyxl import load_workbook
import yaml

with open('config.yml', 'r', encoding='utf-8') as f:
    config = yaml.safe_load(f)

class GetZrksTxt:
    def __init__(self, zrksId):
        self.zrksId = zrksId
        self.zrksFilePath = config['home_dir'] + '/参考.xlsx'

    def getZrksCodeName(self):
        # 读取一个excel文件
        wb = load_workbook(self.zrksFilePath)
        # 名为sheet1的工作表
        ws = wb['zrks']
        
        # 读取第一列和第二列，返回一个json，第一列为code，第二列为name
        zrks_code = []
        first_column = ws['A']
        first_column_values = [cell.value for cell in first_column]
        second_column = ws['B']
        second_column_values = [cell.value for cell in second_column]
        for i in range(len(first_column_values)):
            if first_column_values[i] and second_column_values[i]:
                zrks_code.append({"code": first_column_values[i], "name": second_column_values[i]})
        return zrks_code

    def doGet(self):
        
        # 读取一个excel文件
        wb = load_workbook(self.zrksFilePath)
        ws = wb.active  # 获取活动工作表
        
        # 获取zrksId（第一列）所在的行号
        rowNum = None
        for i in range(1, ws.max_row + 1):
            if ws.cell(row=i, column=1).value == self.zrksId:
                rowNum = i
                break
        if rowNum is None:
            return ""
        
        # 获取zrks_txt
        zrks_txt = ws.cell(row=rowNum, column=3).value
        if zrks_txt:
            return zrks_txt
        else:
            return ""

# if __name__ == "__main__":
#     getZrksTxt = GetZrksTxt("dzjczx")
#     print(getZrksTxt.getZrksCodeName())
